package com.itheima05;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        // 获取 class 对象
        Class<?> c = Class.forName("com.itheima02.Student");

        // 获取无参构造函数创建对象
        Constructor<?> constructor = c.getConstructor();
        Object o = constructor.newInstance();

        // s.method1();
        Method method1 = c.getMethod("method1");
        method1.invoke(o);

        // s.method2("林青霞");
        Method method2 = c.getMethod("method2", String.class);
        method2.invoke(o, "林青霞");

        // s.method3("林青霞", 30);
        Method method3 = c.getMethod("method3", String.class, int.class);
        Object result = method3.invoke(o, "林青霞", 30);
        System.out.println(result);

        // s.function();
        Method function = c.getDeclaredMethod("function");
        function.setAccessible(true);
        function.invoke(o);
    }
}
